package com.bjqg.bjqg01.bjqg.activity.longrange.utils;

import android.database.Cursor;

import com.bjqg.bjqg01.bjqg.activity.longrange.bean.SccjBean;
import com.bjqg.bjqg01.bjqg.activity.longrange.bean.WtrBean;
import com.bjqg.greendao.DaoSession;
import com.bjqg.greendao.SccjBeanDao;
import com.bjqg.greendao.WtrBeanDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bjqg01 on 2018/9/14.
 */

public class WtrBeanUtils {

    private static final String SQL_DISTINCT_ENAME = "SELECT DISTINCT "+ WtrBeanDao.Properties.Name.columnName+" FROM "+WtrBeanDao.TABLENAME;

    public static List<String> listEName(DaoSession session) {
        ArrayList<String> result = new ArrayList<String>();
        Cursor c = session.getDatabase().rawQuery(SQL_DISTINCT_ENAME, null);
        try{
            if (c.moveToFirst()) {
                do {
                    result.add(c.getString(0));
                } while (c.moveToNext());
            }
        } finally {
            c.close();
        }
        return result;
    }
    //插入数据
    public static void insertdata(String name) {
        WtrBean insertData = new WtrBean(null, name);
        getUserDao().insertOrReplace(insertData);
    }


    //查询所有数据
    public static List<String> querydata() {
        //查询数据详细
        List<WtrBean> users = getUserDao().loadAll();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < users.size(); i++) {
            list.add(users.get(i).getName());
        }
        return list;
    }


    /**
     * 删除所有数据
     */
    public static void deleteAllNote() {
        getUserDao().deleteAll();
    }

    /**
     * 根据用户类,删除信息
     *
     * @param user 用户信息类
     */
    public static void deleteNote(WtrBean user) {
        getUserDao().delete(user);
    }


    private static WtrBeanDao getUserDao() {
        return WtrBeanManager.getInstance().getSession().getWtrBeanDao();
    }
}
